Get the depth of a dictionaryΒΆ

Get the depth of a dictionary.
def dict_depth(D):
    if isinstance(D, dict):
        return 1 + (max(map(dict_depth, D.values())) if D else 0)
    return 0

# test
D = {'a': 1, 'b': {'c': {'d': {}}}}
print(dict_depth(D))    # 4